<?php

 // ------------ lixlpixel recursive PHP functions -------------
 // scan_directory_recursively( directory to scan, filter )
 // expects path to directory and optional an extension to filter
 // ------------------------------------------------------------
 function scan_directory_recursively($directory, $filter=FALSE)
 {
     if(substr($directory,-1) == '/')
     {
         $directory = substr($directory,0,-1);
     }
     if(!file_exists($directory) || !is_dir($directory))
     {
         return FALSE;
     }elseif(is_readable($directory))
     {
         $directory_tree = array();
         $directory_list = opendir($directory);
         while($file = readdir($directory_list))
         {
             if($file != '.' && $file != '..')
             {
                 $path = $directory.'/'.$file;
                 if(is_readable($path))
                 {
                     $subdirectories = explode('/',$path);
                     if(is_dir($path))
                     {
                         $directory_tree[] = array(
                             'path'      => $path,
                             'name'      => end($subdirectories),
                             'kind'      => 'directory',
                             'content'   => scan_directory_recursively($path, $filter));
                     }elseif(is_file($path))
                     {
                         $extension = end(explode('.',end($subdirectories)));
                         if($filter === FALSE || $filter == $extension)
                         {
                             $directory_tree[] = array(
                             'path'        => $path,
                             'name'        => end($subdirectories),
                             'extension' => $extension,
                             'size'        => filesize($path),
                             'kind'        => 'file');
                         }
                     }
                 }
             }
         }
         closedir($directory_list);
         return $directory_tree;
     }else{
         return FALSE;
     }
 }
 // ------------------------------------------------------------

?>